/**********************************************************************
  The MIT License (MIT)
  Copyright (c) 2012 sunriseyuen

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************/

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;

using Muki.Framework;
using Muki.Framework.Util;
using Muki.Framework.Business;
using Muki.Entity;
using Muki.UserDefine;

using Dot.StreamObject;

namespace Muki.UserPanel
{
	public class UID03103AM
	{
		public UID03103AM()
		{
		}

		public void Initialize()
		{
//			objDefault.ZZLOGON_SSD = this.ZZLOGON_SSD;
	//		objDefault.Trans       = this.Trans;
		//	objDefault.ZZLOGON_ID  = this.ZZLOGON_ID;

			_Captions.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_Captions.DbName      = this.DbName;

			_Convert.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_Convert.Trans       = this.Trans;

			_DataPermission.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_DataPermission.Trans       = this.Trans;
			_DataPermission.TABLE_NAME  = "PORHDTM";
			_DataPermission.ZZLOGON_ID  = this.ZZLOGON_ID;
			_DataPermission.DataAccessRight();

			_DocsPermission.ZZLOGON_SSD  = this.ZZLOGON_SSD;
			_DocsPermission.Trans        = this.Trans;

			//_Setting.ZZLOGON_ID = this.ZZLOGON_ID;
			//_Setting.Trans      = this.Trans;

		}

		public UID03103AE Process(UID03103AE _Entity)
		{

			UID03103AE _NewEntity = new UID03103AE();

			_WXFLite.XDeclareStart();
			//_WXFLite.ZZLOGON_SSD=this.ZZLOGON_SSD;

			ZZSetting oZZSetting = new ZZSetting();
			oZZSetting.ZZLOGON_ID = this.ZZLOGON_ID;
			oZZSetting.Trans      = this.Trans;

			_WXFLite.Variable("Variables.ZZCOMP_CODE"       , oZZSetting.getValue("ZZCOMP_CODE","ZZCOMP_CODE"));
			_WXFLite.Variable("Variables.ZZCOMP_NAME"       , oZZSetting.getValue("ZZCOMP_CODE","ZZCOMP_CODE"));
			_WXFLite.Variable("Variables.ZZUID_CODE"        , "UID03103A");
			_WXFLite.Variable("Variables.ZZLOGON_ID"        , this.ZZLOGON_ID);
			_WXFLite.Variable("Variables.ZZDATE_TIME"       , DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
			_WXFLite.Variable("Variables.ZZCONTINUE"        , _Captions.Caption("lbl_ZZCONTINUE"));
			_WXFLite.Variable("Variables.ZZFINISH"          , _Captions.Caption("lbl_ZZFINISH"));
			_WXFLite.Variable("Variables.ZZRANGE_FM"        , _Captions.Caption("lbl_ZZRANGE_FM"));
			_WXFLite.Variable("Variables.ZZRANGE_TO"        , _Captions.Caption("lbl_ZZRANGE_TO"));
			_WXFLite.Variable("Variables.ZZPRINT_DATE"      , _Captions.Caption("lbl_ZZPRINT_DATE"));
			_WXFLite.Variable("Variables.ZZPRINT_TIME"      , _Captions.Caption("lbl_ZZPRINT_TIME"));
			_WXFLite.Variable("Variables.DateTime_MinValue" , DateTime.MinValue.ToString("1900-MM-dd"), "datetime");
			_WXFLite.Variable("XCaption.ZZCOMP_CODE"        , _Captions.Caption("lbl_ZZCOMP_CODE"));
			_WXFLite.Variable("XCaption.Page_No"            , _Captions.Caption(this.UID_CODE  , "lbl_Page_No"));
			_WXFLite.Variable("XCaption.ZZLOGON_ID"         , _Captions.Caption("lbl_LOGON_ID"));
			_WXFLite.Variable("XCaption.ZZUID_CODE"         , _Captions.Caption("lbl_UID03103A"));

				QueryRows RsUID03103A;

				objZZQLite = new ZZQLite();

				ZZSQLStatement objZZSQLStatement;
				ZZRefx objZZRefx;

				RsUID03103A = new QueryRows(this.Trans);

				objZZSQLStatement             = new ZZSQLStatement();
				objZZSQLStatement.Trans       = this.Trans;
				objZZSQLStatement.DbName      = this.DbName;
				objZZSQLStatement.ZZLOGON_SSD = this.ZZLOGON_SSD;

				objZZRefx           = new ZZRefx();
				objZZRefx.UGET_CODE = "QDD03103A";
				objZZRefx.Trans     = this.Trans;
				objZZRefx.Refx();

				ZZTrace.Debug(ZFILE_NAME, objZZRefx.USQL_CODE);

				objZZSQLStatement.UID_CODE = this.UID_CODE;
				objZZSQLStatement.Process(objZZRefx.USQL_CODE);

				objZZQLite = objZZSQLStatement.oZZQLite;
				objZZQLite.ZZLOGON_SSD = this.ZZLOGON_SSD;

				string cAccessName = "";
					foreach (ZZMapping objMapping in objZZRefx.Ref_Mapping)
					{
						cAccessName = "Txt_" + objMapping.ACCESS_TABLE_NAME + "_" + objMapping.ACCESS_COLUMN_NAME;

						_WXFLite.Variable("XWhere."+objMapping.TABLE_NAME+"."+objMapping.COLUMN_NAME,ZZGeneral.XRequest(cAccessName));

						objZZQLite.AddXWhereClause(objMapping.TABLE_NAME, objMapping.COLUMN_NAME,ZZGeneral.XRequest(cAccessName));
					}

				foreach(AttributeMapping _AttributeMapping in objZZQLite.AttributeMappings)
				{
					//AttributeMapping objaColumn =_AttributeMapping;
					//objaColumn.Caption = _Captions.Caption(this.UID_CODE, objaColumn.Caption);
					
					ZZTrace.Debug(ZFILE_NAME, "_AttributeMapping="+_AttributeMapping.Caption);

					_WXFLite.Variable("XCaption."+_AttributeMapping.Name , _AttributeMapping.Caption);

					_WXFLite.XDeclare(_AttributeMapping);
				}

				_WXFLite.XDeclareFinal();

				objZZQLite.Make();

				string cSQLString = objZZQLite.SQLStatement;

				ZZTrace.Debug(ZFILE_NAME,cSQLString);

				RsUID03103A.CommandText = cSQLString;
				RsUID03103A.Open();

				_Captions.ZZLOGON_SSD  =this.ZZLOGON_SSD;

				while (!RsUID03103A.EOF)
				{
					_WXFLite.AddNew();
					int i=0;
					foreach(AttributeMapping _AttributeMapping in objZZQLite.AttributeMappings)
					{
						_WXFLite[_AttributeMapping.Name]  = RsUID03103A[i];
						i++;
					}

					_WXFLite.Update();

					RsUID03103A.MoveNext();
				}

				ZZTrace.Debug(ZFILE_NAME, "==**==");
			
			int _PageSize=20;
			int _TotalPages = _WXFLite.RecordCount / _PageSize;
			if (_WXFLite.RecordCount > (_WXFLite.RecordCount / _PageSize) * _PageSize)
			{
				_TotalPages = _TotalPages + 1;
			}

			_WXFLite.Variable("ZZPageSize",_PageSize);
			_WXFLite.Variable("ZZPages",_TotalPages);

			XDataCache oXDataCache  = new XDataCache();
			oXDataCache.DataUrl     = ZZGeneral.UUID();
			oXDataCache.APP_PATH    = this.APP_PATH;
			oXDataCache.ZZLOGON_ID  = this.ZZLOGON_ID;
			oXDataCache.ZZLOGON_SSD = this.ZZLOGON_SSD;
			oXDataCache.UID_CODE    = "UID03103A";
			oXDataCache.ZZLOGON_TP  = "t";
			oXDataCache.Context     = _WXFLite;
			oXDataCache.DbName      = this.DbName;
			oXDataCache.Write();

			_NewEntity.DataUrl=oXDataCache.DataUrl;
			_NewEntity.Wxf=_WXFLite;
			
			return _NewEntity;
		}

		public UID03103AE Convert(UID03103AE _Entity)
		{
		
			string PrintableDocument=this.APP_PATH+"cfg\\PrintableDocument.xml";

			if (!File.Exists(PrintableDocument))
			{
				PrintableDocument=this.APP_PATH+"cfg\\PrintableDocument.config";
			}

			ZZTrace.Debug(ZFILE_NAME,PrintableDocument);

			if (File.Exists(PrintableDocument))
			{

				StringBuilder XCodeObject = new StringBuilder(); 

				if (File.Exists(PrintableDocument))
				{
					XCodeObject=ZZGeneral.ReadStream(PrintableDocument);
				}

				VContexts _VContexts=new VContexts(XCodeObject.ToString());

				string DefaultPrintableDocument   = _VContexts.getValue("DefaultPrintableDocument");
				string cExtensions="";
				if (string.IsNullOrEmpty(DefaultPrintableDocument))
				{
					DefaultPrintableDocument="ll";
				}

				string DefaultPrintableExtensions= _VContexts.getValue(DefaultPrintableDocument);

				cExtensions=this.APP_PATH+"System\\Extensions\\"+DefaultPrintableExtensions+".dll";

				ZZTrace.Debug(ZFILE_NAME,cExtensions);

				if (File.Exists(cExtensions))
				{
					System.Reflection.Assembly objAssembly = Dot.StreamObject.Util.ReadAssembly(cExtensions);

					PrintableDocument oPrintableDocument = (PrintableDocument)objAssembly.CreateInstance(DefaultPrintableExtensions);

					string Template=this.APP_PATH+"Layouts\\Default\\UID03103A.lst";

					oPrintableDocument.ZZLOGON_LANG=this.ZZLOGON_LANG;
					oPrintableDocument.UID_CODE  ="UID03103A";
					oPrintableDocument.APP_PATH  = this.APP_PATH;
					oPrintableDocument.Trans=Trans;
					oPrintableDocument.Template=Template;
					oPrintableDocument.WXFLite=_Entity.Wxf;
					oPrintableDocument.Execute();

					_Entity.FileUrl=oPrintableDocument.FileUrl;

					ZZTrace.Debug(ZFILE_NAME,_Entity.FileUrl);
					ZZTrace.Debug(ZFILE_NAME,this.APP_PATH);
					ZZTrace.Debug(ZFILE_NAME,Template);
					ZZTrace.Debug(ZFILE_NAME,_Entity.Wxf.ToString());
				}
			}
			return _Entity;
		}
#region Field

		const string ZFILE_NAME = "UID03103AM";
		private ZZQLite  objZZQLite;
		private ZZDataPermission _DataPermission = new ZZDataPermission();
		private ZZDocsPermission _DocsPermission = new ZZDocsPermission();
		private string   _FileName               = "UID03103A";
		private XConvert   _Convert              = new XConvert();
		private Wxf _WXFLite                     = new Wxf();
		private ZZCaptions   _Captions           = new ZZCaptions();
		private string         _APP_PATH           = "";

#endregion

#region Properties

    private string _ZZLOGON_ID  = "";
    private string _UID_CODE    = "";
    private string _DbName      = "";
    private string _ZZLOGON_SSD = "";
    private string _LOGON_LANG  = "";
    private StreamContext _Trans;

    public StreamContext Trans      { get { return _Trans;        } set { _Trans         = value; }  }
    public string UID_CODE          { get { return _UID_CODE;     } set { _UID_CODE      = value; }  }
    public string ZZLOGON_ID        { get { return _ZZLOGON_ID;   } set { _ZZLOGON_ID    = value; }  }
		public string  APP_PATH          { get { return _APP_PATH;          } set { _APP_PATH          = value; }  }
    public string ZZLOGON_LANG        { get { return _LOGON_LANG;   } set { _LOGON_LANG    = value; }  }
    public string ZZLOGON_SSD       { get { return _ZZLOGON_SSD;  } set { _ZZLOGON_SSD   = value; }  }
		public Wxf WXFLite     { get { return _WXFLite;  } set { _WXFLite = value;  }  }
		public string FileName { get { return _FileName; } set { _FileName = value; }  }
		public string DbName { get { return _DbName; } set { _DbName = value; }  }

#endregion

	}
}
